package com.example.qu_tao.adapter;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.qu_tao.R;
import com.example.qu_tao.View.GoodsDetailActivity;
import com.example.qu_tao.database.DBHelper;
import com.example.qu_tao.entity.Goods;
import com.example.qu_tao.entity.Photo;


import java.util.List;

public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.ViewHolder> {

   private List<Goods> mList;
   private Context mContext;
   private DBHelper dbHelper;
   public GoodsListAdapter(Context context,List<Goods> list){
      mContext = context;
      mList = list;
      dbHelper = new DBHelper(context);
   }

   public void updateData(List<Goods> list) {
      mList.clear();
      mList = list;
      notifyDataSetChanged();
   }

   @NonNull
   @Override
   public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shouye_item, null);
      return new ViewHolder(view);
   }

   public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
      Goods goods = mList.get(position);
      holder.textView1.setText(goods.getGoods_name());
      holder.textView2.setText("¥ " + goods.getGoods_price() + "/" + goods.getUnit());

      List<Photo> photoList = goods.getPhotoList();
      if (photoList != null && !goods.getPhotoList().isEmpty()) {
         byte[] imageData = goods.getPhotoList().get(0).getData();
         if (imageData != null && imageData.length > 0) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
            holder.photo.setImageBitmap(bitmap);
         } else {
            holder.photo.setImageResource(R.mipmap.ic_launcher);
         }
      } else {
         holder.photo.setImageResource(R.mipmap.ic_launcher);
      }

      holder.itemView.setOnClickListener(v -> {
         // 插入数据到足迹表
         insertToFootprint(goods);
         Intent intent = new Intent(mContext, GoodsDetailActivity.class);
         intent.putExtra("goodsId", goods.getGoods_id()); // 假设Goods类有getGoods_id()方法
         mContext.startActivity(intent);
      });
   }

   private void insertToFootprint(Goods goods) {
      SQLiteDatabase db = dbHelper.getWritableDatabase();
      String insertQuery = "INSERT INTO " + DBHelper.TABLE_NAME_FOOTPRINT +
              " (goods_id, goods_name, goods_price, goods_address, goods_content) " +
              "VALUES (" + goods.getGoods_id() + ", '" + goods.getGoods_name() + "', " +
              goods.getGoods_price() + ", '" + goods.getGoodsAddress() + "', '" +
              goods.getGoods_content() + "')";
      db.execSQL(insertQuery);
      db.close();
   }

   @Override
   public int getItemCount() {
      return mList.size();
   }

   public static class ViewHolder extends RecyclerView.ViewHolder {
      ImageView photo;
      TextView textView1;
      TextView textView2;

      public ViewHolder(@NonNull View itemView) {
         super(itemView);
         photo = itemView.findViewById(R.id.shouye_item_image);
         textView1 = itemView.findViewById(R.id.shouye_item_name);
         textView2 = itemView.findViewById(R.id.shouye_item_price);
      }
   }
}
